"""
@author: 江同学呀
@file: geo_shape_field.py
@date: 2024/7/28 19:40
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/geo-shape.html

    Complex shapes, such as polygons.
    复杂形状，例如多边形。
"""
from abc import ABC
from typing import Type, Union, Dict

from espc.common.field_common import PrefixTree, Precision, Strategy, Orientation
from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_geo_field import _BaseGeoField
from espc.orm.model.mapping.field.field_attribute import IgnoreZValueAttribute, IgnoreMalformedAttribute, CoerceAttribute, \
    TreeAttribute, PrecisionAttribute, TreeLevelsAttribute, StrategyAttribute, DistanceErrorPctAttribute, \
    OrientationAttribute, PointsOnlyAttribute
from espc.utils.build_add_attr import build_add_attr


class GeoShapeField(
    _BaseGeoField, TreeAttribute, PrecisionAttribute, TreeLevelsAttribute, StrategyAttribute, DistanceErrorPctAttribute,
    OrientationAttribute,  PointsOnlyAttribute, IgnoreMalformedAttribute, IgnoreZValueAttribute, CoerceAttribute, ABC
):
    """
    形状 字段
    """
    _field_type: str = "geo_shape"

    def __init__(
            self, field_name: str = None, value: Dict = None, default: Dict = None,
            tree: Union[str, PrefixTree] = None, coerce: bool = None, precision: Union[str, Precision] = None,
            tree_levels: int = None, strategy: Union[str, Strategy] = None, distance_error_pct: float = None,
            orientation: Union[str, Orientation] = None, points_only: bool = None, ignore_z_value: bool = None,
            ignore_malformed: bool = None
    ):
        super(GeoShapeField, self).__init__(
            field_name=field_name, value=value, default=default, ignore_malformed=ignore_malformed,
            ignore_z_value=ignore_z_value, tree=tree, precision=precision, tree_levels=tree_levels, strategy=strategy,
            orientation=orientation, distance_error_pct=distance_error_pct, coerce=coerce, points_only=points_only
        )
        return

    def __get__(self, instance: _Base, owner: Type["GeoShapeField"]) -> Union["GeoShapeField", Dict]:
        return super(GeoShapeField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: Dict):
        super(GeoShapeField, self).__set__(instance=instance, value=value)
        return

    def __getattribute__(self, item):
        return super(GeoShapeField, self).__getattribute__(item)

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping

